package com.boc.langxiang.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SplashActivity extends BaseActivity implements Handler.Callback, ViewPager.OnPageChangeListener {

    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.ll)
    LinearLayout mLl;
    @BindView(R.id.use)
    TextView mUse;

    private Handler mHandler;

    private ArrayList<View> views;

    private static final int[] pics = {R.drawable.splash_one, R.drawable.splash_two, R.drawable.splash_three};

    // 底部小点的图片
    private ImageView[] points;
    // 记录当前选中位置
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        initData();

        mHandler = new Handler(this);
        mHandler.sendEmptyMessageDelayed(100, 2000);
    }

    private void initData() {
        views = new ArrayList<>();
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        for (int i = 0; i < pics.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
//            iv.setImageResource(pics[i]);
//            iv.setOnClickListener(this);
            iv.setTag(i);
            Picasso.with(mContext)
                    .load(pics[i])
                    .into(iv);

            views.add(iv);
        }

        mViewpager.setOnPageChangeListener(this);
        mViewpager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return views.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return (view == object);
            }

            @Override
            public void destroyItem(View container, int position, Object object) {
                ((ViewPager) container).removeView(views.get(position));
            }

            @Override
            public Object instantiateItem(View container, int position) {
                ((ViewPager) container).addView(views.get(position), 0);
                return views.get(position);
            }
        });

        initPoint();
    }

    private void initPoint() {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);

        points = new ImageView[pics.length];

        for (int i = 0; i < pics.length; i++) {
            points[i] = (ImageView) linearLayout.getChildAt(i);
            points[i].setEnabled(true);
            points[i].setTag(i);
        }

        currentIndex = 0;
        points[currentIndex].setEnabled(false);
    }

    private void setCurDot(int position) {
        if (position < 0 || position > pics.length - 1 || currentIndex == position) {
            return;
        }
        if (position == 2) {
            mUse.setVisibility(View.VISIBLE);
            mLl.setVisibility(View.GONE);
        } else {
            mUse.setVisibility(View.GONE);
            mLl.setVisibility(View.VISIBLE);
        }
        points[position].setEnabled(false);
        points[currentIndex].setEnabled(true);

        currentIndex = position;
    }

    @Override
    public boolean handleMessage(Message msg) {
        return false;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setCurDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        mViewpager.setCurrentItem(position);
    }

    @OnClick(R.id.use)
    public void onClick() {
        startActivity(new Intent(mContext, MainActivity.class));
        finish();
    }
}
